package com.bgs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bgs.pojo.EmployeeInfo;
import com.bgs.pojo.EmployeeRegister;
import com.bgs.utils.MyContent;
import org.apache.commons.lang3.StringUtils;
import com.bgs.data.RegisterVO;
import com.bgs.service.ERegisterService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ERegisterController {

    Logger log = LogManager.getLogger();

    @Autowired
    private ERegisterService eRegisterService;


    @RequestMapping("/lo")
    public String lo(){
        return "login";
    }

    @RequestMapping("/lot")
    public String lot(){
        return "register";
    }

    //用户登录
    @RequestMapping("/login")
    public String login(EmployeeRegister employeeRegister, Model model, HttpSession session){
        log.info("loginloginlogin" +employeeRegister.getEmail());
        log.info("loginloginlogin" +employeeRegister.getPassword());
        EmployeeRegister er = eRegisterService.login(employeeRegister);
        if (er != null){
//            model.addAttribute("eRegister",er);
            session.setAttribute("user",er);
            return "welcome";
        }else {
            model.addAttribute("error","账号或者密码不正确");
        }

        return "login";
    }

    //注册
    @RequestMapping("/register")
    public String register(RegisterVO registerVO, Model model) {
//    public String register(String username,String email,String password,String confimpassword){

        //1、接收参数
        //2、参数校验
        //3、调用service接口

        //1
        log.info("register begin");
        String username = StringUtils.trimToEmpty(registerVO.getUsername());
        log.info("register username: " + username);
        String email = StringUtils.trimToEmpty(registerVO.getEmail());
        log.info("register email: " + email);
        String password = StringUtils.trimToEmpty(registerVO.getPassword());
        log.info("register password: " + password);

        String confimpassword = registerVO.getConfimpassword();
        log.info("register password: " + confimpassword);
        // 判断两次输入密码是否一致
        if (!StringUtils.equals(password, confimpassword)) {
            // 密码不一样
            model.addAttribute(MyContent.CODE, "9999");
            model.addAttribute(MyContent.MSG, "两次密码不一致");
            return "fail";
        }

        //2、调用serviece接口
        Map<String,Object> result = eRegisterService.saveRegister(username,email,password);

        if ("0000".equals(result.get("code"))){
            //注册成功
            return "login";
        }

        return "fail";
    }

    //测试ajax
    @ResponseBody
    @RequestMapping("/test")
    public Map<String,Object> test(@RequestBody EmployeeInfo employeeInfo){

        Map<String,Object> m = new HashMap<>();

        log.info("employeeInfo::::"+employeeInfo);

        m.put("code","0000");
        m.put("msg","交易成功");
        return m;
    }

    //测试ajax的String d
    @ResponseBody
    @RequestMapping("test1")
    public Map<String,Object> test1(@RequestBody String str) {
        log.info("test1::::" + str);

        Map<String, Object> m = new HashMap<>();
        JSONObject jsonObj = JSONObject.parseObject(str);
        String companyName = jsonObj.getString("company_name");
        log.info(companyName);
        System.out.println("!!!!!!!!!@!!");
        JSONArray jsonArray = jsonObj.getJSONArray("dept");

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject dept = jsonArray.getJSONObject(i);
            if ("女".equals(dept.getString("sex"))) {
                log.info("name::::" + dept.getString("name"));
                log.info("sex:::::" + dept.getString("sex"));
                log.info("email:::" + dept.getString("email"));
            }

        }
        log.info("dddddddd---ddd" + jsonArray);

        return m;
    }

    //测试ajax的String qq
    @ResponseBody
    @RequestMapping("test2")
    public Map<String,Object> test2(@RequestBody String str) {
        log.info("test2::::" + str);

        Map<String, Object> m = new HashMap<>();
        JSONObject jsonObj = JSONObject.parseObject(str);

        JSONObject jsonObj1 = jsonObj.getJSONObject("response");
        JSONObject jsonObj2 = jsonObj1.getJSONObject("data");
        JSONArray jsonArray = jsonObj2.getJSONArray("cnameRecords");

//        String companyName = jsonObj.getString("company_name");
//        log.info(companyName);
//        System.out.println("!!!!!!!!!@!!");
//        JSONArray jsonArray = jsonObj.getJSONArray("response.data.cnameRecords");

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject dept = jsonArray.getJSONObject(i);
            String canonicalHost = dept.getString("canonicalHost");
            String aliasHost = dept.getString("aliasHost");
//            if (canonicalHost.matches(".*webmail.*") || aliasHost.matches(".*webmail.*")) {
            if (canonicalHost.contains("webmail")) {
                log.info("canonicalHost::::" + dept.getString("canonicalHost"));
                log.info("aliasHost:::::" + dept.getString("aliasHost"));
            }

        }
        log.info("dddddddd---ddd" + jsonArray);

        return m;
    }

}
